package com.woniuxy01;


import java.io.*;

/**
 * @author 作者
 * @create 2025-09-16 10:41
 */
public class Test02 {

    public static void main(String[] args) throws IOException {

    }

    private static void test01() throws IOException {
        long begin = System.currentTimeMillis();
        //????
        //3003
        //1128
        //275
        copyFile04("d:\\aaa\\a.mp4","d:\\aaa\\b.mp4");
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }

    public static void copyFile06(String source,String target) throws IOException {
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));) {
            byte[] bytes=new byte[100];
            int length=-1;
            while ((length=bis.read(bytes))!=-1) {
                bos.write(bytes,0,length);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        }
    }


    public static void copyFile05(String source,String target)  {
        BufferedInputStream bis = null;
        BufferedOutputStream bos=null;
        try {
            bis = new BufferedInputStream(new FileInputStream(source));
            bos=new BufferedOutputStream(new FileOutputStream(target));
            byte[] bytes=new byte[100];
            int length=-1;
            while ((length=bis.read(bytes))!=-1) {
                bos.write(bytes,0,length);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    //带缓冲区字节流：复制一次读取一个字节数组
    public static void copyFile04(String source,String target) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(target));
        byte[] bytes=new byte[100];
        int length=-1;
        while ((length=bis.read(bytes))!=-1) {
            bos.write(bytes,0,length);
        }
        bos.close();
        bis.close();
    }
    //带缓冲区字节流：复制一次读取一个字节
    public static void copyFile03(String source,String target) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        int mybyte = -1;
        while ((mybyte = bis.read()) != -1) {
            bos.write(mybyte);
        }
        bos.close();
        bis.close();

    }

    //字节流：复制一次读取一个字节数组
    public static void copyFile02(String source,String target) throws IOException {
        FileInputStream fis=new FileInputStream(source);
        FileOutputStream fos=new FileOutputStream(target);
        byte[] bytes=new byte[100];
        int length=-1;
        while ((length=fis.read(bytes))!=-1) {
            fos.write(bytes,0,length);
        }
        fos.close();
        fis.close();
    }

    //字节流：复制一次读取一个字节
    public static void copyFile01(String source,String target) throws IOException {
        FileInputStream fis=new FileInputStream(source);
        FileOutputStream fos=new FileOutputStream(target);
        int mybyte=-1;
        while ((mybyte=fis.read())!=-1) {
            fos.write(mybyte);
        }
        fos.close();
        fis.close();
    }
}
